#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=33;

int h,wid;
char arr[N][N];
//int w[N][N],e[N][N],n[N][N],s[N][N];
LL cost[4][N][N];
int r0,c0,way0;
int rf,cf;
LL ans;
int dr[]={0,-1,0,1};
int dc[]={1,0,-1,0};
int shift[4][4]={
    {0,1,10,5},{5,0,1,10},{10,5,0,1},{1,10,5,0}
};
const map<char,int> waymap={
    {'E',0},{'N',1},{'W',2},{'S',3}
};

void dfs(int r,int c,int way){
    if(r==rf&&c==cf){
        ans=min(ans,cost[way][r][c]);
        return;
    }
    for(int i=0;i<4;i++)
    {
        int r1=r+dr[i],c1=c+dc[i];
        if(r1<=0||r1>h) continue;
        if(c1<=0||c1>wid) continue;
        if(arr[r1][c1]=='.') continue;
        
        int co=shift[way][i];
        if(cost[i][r1][c1]>cost[way][r][c]+co){
            cost[i][r1][c1]=cost[way][r][c]+co;
            if(cost[i][r1][c1]>ans) return;
            dfs(r1,c1,i);
        }
    }
}

int main()
{
    cin>>h>>wid;
    for(int i=1;i<=h;i++){
        string str;
        cin>>str;
        for(int j=0;str[j]!='\0';j++){
            arr[i][j+1]=str[j];
            if(str[j]=='E'||str[j]=='W'||str[j]=='N'||str[j]=='S'){
                r0=i;
                c0=j+1;
                way0=waymap.at(str[j]);
                arr[i][j+1]='#';
            }
            else if(str[j]=='F'){
                rf=i;
                cf=j+1;
                arr[i][j+1]='#';
            }
        }
    }
    memset(&ans,0x7f,sizeof(ans));
    memset(cost,0x3f,sizeof(cost));
    cost[way0][r0][c0]=0;
    dfs(r0,c0,way0);
    cout<<ans;
    return 0;
}